package com.banciyuan.bcywebview.biz.post.write;

import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.api.NovelApi;
import com.banciyuan.bcywebview.base.factory.TimelineFactory;
import com.banciyuan.bcywebview.base.helper.data.UserDataHelper;
import com.banciyuan.bcywebview.base.view.toast.MyToast;
import com.banciyuan.bcywebview.biz.detail.noveldetail.newnoveldetail.NovelComplexActivity;
import com.banciyuan.bcywebview.utils.constants.MyConstants;
import com.banciyuan.bcywebview.utils.http.HttpUtils;
import com.banciyuan.bcywebview.utils.http.StatusCodeUtils;
import com.banciyuan.bcywebview.utils.http.StringRequestParam;
import com.banciyuan.bcywebview.utils.intent.gotoUtil;
import com.google.gson.Gson;

import com.banciyuan.bcywebview.utils.http.BcyPair;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import de.greenrobot.daoexample.model.DetailType;

/**
 * Created by squall on 2016/2/22.
 */
public class WriteEditActivity extends WritePostActivity {

    private DetailType detailData;

    @Override
    protected void initArgs() {
        super.initArgs();
        detailData = (DetailType) getIntent().getSerializableExtra("detail");
    }

    @Override
    protected void initActionbar() {
        super.initActionbar();
        mActionbarHelper.setTitle(getString(R.string.edit_write));
    }

    @Override
    protected void initUi() {
        super.initUi();
        ((TextView) findViewById(R.id.info_text)).setText(getString(R.string.add_title_write_no_tags));
        if (postItem.getWns_id() != null) {
            findViewById(R.id.serise_warn).setVisibility(View.VISIBLE);
        }
    }

    private void commit() throws Exception {
        mDialog.show();
        String url = HttpUtils.BASE_URL + NovelApi.getDoeditnovel();
        List<BcyPair> pairs = new ArrayList<BcyPair>();
        pairs.add(new BcyPair(HttpUtils.WPID, detailData.getWp_id()));
        pairs.add(new BcyPair(HttpUtils.RPID, detailData.getRp_id()));
        pairs.add(new BcyPair(HttpUtils.USER_TOKEN, UserDataHelper.getInstance(WriteEditActivity.this).getToken()));
        pairs.add(new BcyPair(HttpUtils.UPLOAD_DATA, getJson()));
        HashMap<String, String> map = HttpUtils.getData(pairs);

        Response.Listener<String> onSucc = new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                if (!WriteEditActivity.this.isFinishing()) {
                    mDialog.dismiss();
                }
                try {
                    if (StatusCodeUtils.dealStatusCode(s, WriteEditActivity.this)) {
                        Intent intent = new Intent();
                        intent.setClass(WriteEditActivity.this, NovelComplexActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        intent.putExtra(MyConstants.WPIDVALUE, detailData.getWp_id());
                        intent.putExtra(MyConstants.RPIDVALUE, detailData.getRp_id());
                        intent.putExtra(MyConstants.TIMELINETYPE, TimelineFactory.TIMELINE_TYPE_WRITER);
                        startActivity(intent);
                    }
                } catch (Exception e) {
                    MyToast.show(WriteEditActivity.this, getString(R.string.modify_fail));
                }
            }
        };

        Response.ErrorListener onError = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                MyToast.show(WriteEditActivity.this, getString(R.string.modify_fail));
                if (!WriteEditActivity.this.isFinishing()) {
                    mDialog.dismiss();
                }
            }
        };
        StringRequestParam stringRequestParam = new StringRequestParam(Request.Method.POST, url, map, onSucc, onError);
        mQueue.add(stringRequestParam);

    }

    private String getJson() {
        Gson gson = new Gson();
        postItem.setWork(et_work.getText().toString().trim());
        postItem.setCover(writeCoverList.get(selectCoverIndex).getCover());
        return gson.toJson(postItem);
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()) {
            case R.id.ll_rights_set:
                saveItem();
                intent.setClass(this, WriteRightsActivity.class);
                intent.putExtra(gotoUtil.INTENT_VALUE_ONE, postItem);
                startActivityForResult(intent, GO_SETRIGHTS);
                break;
            case R.id.ll_info_set:
                saveItem();
                intent.setClass(this, WriteInfoActivity.class);
                intent.putExtra(gotoUtil.INTENT_VALUE_ONE, postItem);
                intent.putExtra(gotoUtil.INTENT_VALUE_TWO, false);
                startActivityForResult(intent, GO_SETINFO);
                break;
            case R.id.base_action_bar_home_text_item:
                try {
                    commit();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
        }
    }
}
